<?php

namespace app\admin\controller;

use app\common\model\WinningRecord;
use think\Request;
use app\common\model\Activity as ActivityModel ;

class Activity extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('activity/activitylist')
     * 活动列表
     */
    public function activitylist()
    {
		$get['activity_name'] = input('get.activity_name');
		$get['is_del'] = 1;
		$item = [
		    '*',
            '(select count(winning_id) from ddt_winning_record where ddt_winning_record.activity_id = ddt_activity.activity_id and is_winning <> 1) as winning_number',
            '(select count(winning_id) from ddt_winning_record where ddt_winning_record.activity_id = ddt_activity.activity_id and is_winning <> 1 and is_use <> 1) as draw_number',
        ];
		$ActivityModel = new ActivityModel();
        $Activity = $ActivityModel->get_activity_list($get,implode(',',$item),'activity_id',$this->limit);

        $page = $Activity->render();

        $tvar = array(
            "title" => "活动列表",
            "time" => time(),
            "data" => $Activity,
			'get' => $get,
        );
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('activity/activitylist');
    }

    /**
     * @route('activity/boxeactivity')
     * 编辑添加活动
     */
    public function boxActivity(Request $request)
    {
		$ActivityModel = new ActivityModel();

        $activity_id = input('activity_id');
        $activity = null;
        $tvar = array(
            "title" => "添加活动",
            "time"  => time(),
            "activity_id"   => $activity_id,
        );
        if($activity_id != 0){
            $tvar["title"] = "编辑活动";
            $activity = $ActivityModel->where('activity_id','=',$activity_id)->where('is_del','=',1)->find();
            if(!$activity){
                $this->error('不存的活动');
            }
        }
        if(request()->isAjax()){
            $scopetime = input("post.scope_time/s");
            $scope = explode(" - ",$scopetime);
//            var_dump($scope);die;
            $activity_id = input("post.activity_id/d",0);
            $is_have_activity = $ActivityModel->where('is_del','=',1)->find();
            if(!empty($is_have_activity)){
                $activity_id != 0?$is_have_activity['activity_id'] != $activity_id?$this->error('已存在活动请修改'):'':$this->error('已有活动');
            }
            $param = [
                "activity_title" => input("post.activity_title/s"),
                "address" => input("post.address/s"),
                "phone" => input("post.phone/f"),
                "begin_time" => strtotime($scope[0]),
                "end_time" => strtotime($scope[1]),
                "show_time" => strtotime(input("post.show_time/s")),
                "provide_number" => input("post.provide_number/f"),
            ];
//            var_dump($param);die;
            $rs = $ActivityModel->edit($param,$activity_id);
            if($rs === false){
                $this->error($ActivityModel->getError());
            }
            $this->success('操作成功');
        }


        $this->assign("data", $tvar);
        $this->assign("activity", $activity);
        return $this->fetch('activity/boxeactivity');
    }

    /**
     * @route('activity/delactivity')
     * 删除活动
     */
    public function delactivity()
    {
        $id = input('activity_id');
        if(empty($id)){
            $this->error("不存在的活动记录");
        }
		$ActivityModel = new ActivityModel();
        $rs = $ActivityModel->edit(['is_del'=>2],$id);
        if($rs === false){
            $this->error("删除失败");
        }
        $this->success('删除成功 ');
    }
    
    /**
     * @route('activity/recordlist')
     * 查看活动中奖人
     */
    public function recordlist()
    {
        $activity_id = input('get.activity_id');
        $get['user_name'] = input('get.user_name');
        $get['win_code'] = input('get.win_code');
        $get['activity_id'] = $activity_id;
        $get['is_del'] = 1;

        $activityModel = new ActivityModel();
        $activity = $activityModel->getOne([['activity_id','=',$activity_id],['is_del','=',1]]);

        if(empty($activity)){
            $this->error('不存在该活动');
        }
        $winModel = new WinningRecord();
        $records = $winModel->get_record_list($get,'w.*,u.nick_name,u.avatar','w.add_time desc',$this->limit);
//        echo "<pre>";
//        var_dump($records->toArray());die;
        $tvar = array(
            "title" => "活动中奖列表",
            "time"  => time(),
            "data"  => $records,
            "get"  => $get,
        );
        $page = $records->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('activity/record/recordlist');
    }

    /**
     * @route('activity/sureget')
     * 用户确认领取奖品
     */
    public function sureget()
    {
        $id = input('winning_id');
        if(empty($id)){
            $this->error("不存在的中奖记录");
        }
        $model = new WinningRecord();
        $rs = $model->edit(['is_use'=>2],$id);
        if($rs === false){
            $this->error("操作失败");
        }
        $this->success('操作成功 ');
    }
}